package com.spring.mybatis.mapper;

import com.spring.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserInfoMapper {
//    @Select("select id, username, password, age, gender, phone, " +
//            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
//    List<UserInfo> selectAll();

//    @Results(id = "baseMap",value = {
//            @Result(column = "delete_flag",property = "deleteFlag"),
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime"),
//    })
    @Select("select id, username, password, age, gender, phone,delete_flag, create_time, update_time  from user_info")
    List<UserInfo> selectAll();

    @Select("select * from user_info")
    UserInfo selectAll2();

//    @ResultMap("baseMap")
    @Select("select id, username, password, age, gender, phone,delete_flag, create_time, update_time from user_info where id = #{id}")
    UserInfo selectUserById(Integer id);

// 1
//    @Select("select * from user_info where age = #{age} and gender = #{gender}")
//    List<UserInfo> selectUserByAgeAndGender(Integer age,Integer gender);
// 2
//    @Select("select * from user_info where age = #{param1} and gender = #{param2}")
//    List<UserInfo> selectUserByAgeAndGender(Integer age,Integer gender);
// 3
    @Select("select * from user_info where age = #{age1} and gender = #{gender1}")
    List<UserInfo> selectUserByAgeAndGender(@Param("age1") Integer age,@Param("gender1") Integer gender);


    //传递对象
    @Insert("insert into user_info (username,`password`,age,gender) values(#{username},#{password},#{age},#{gender})")
    Integer insertUserInfo(UserInfo userInfo);
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username,`password`,age,gender) values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insertUserInfo2(@Param("userInfo") UserInfo userInfo);

    //删除数据
    @Delete("delete from user_info where id = #{id}")
    Integer delete(Integer id);

    //修改数据
    @Update("update user_info set password = #{password} where id = #{id}")
    Integer update(Integer id,String password);
//    @Update("update user_info set password = #{password} where id = #{id}")
//    Integer update(UserInfo userInfo);

    @Select("select id, username, password, age, gender, phone,delete_flag, create_time, update_time  from user_info where username='${username}' and password = '${password}'")
    List<UserInfo> selectUserByNameAndPassword(String username,String password);

    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);

    @Select("select * from user_info where username like '%${like}%'")
    List<UserInfo> selectUserByLike(String like);

    @Select("select * from user_info where username like concat('%',#{like},'%')")
    List<UserInfo> selectUserByLike2(String like);
}
